$(function() {
	if (navigator.userAgent.indexOf("MSIE") > 0) {
		document.getElementById('selState').style.width = 25 + 'px';
	} 
	if (navigator.userAgent.indexOf("Firefox") > 0) {
		document.getElementById('selState').style.width = 31 + 'px';
	}
	var http = new XMLHttpRequest;
	http.open("HEAD", ".", false);
	http.send(null);
	var today = new Date(http.getResponseHeader("Date"));
	var year_today = today.getFullYear();
	var month_today = (today.getMonth() + 1) < 10 ? "0" + (today.getMonth() + 1) : (today.getMonth() + 1);
    var time = year_today + "-" + month_today;
	//初始化时间选择器
	$('#searchTime').datepicker({
		autoclose: true,
		todayBtn: true,
		startView: 1,
		minViewMode: 1
	});
	$('#searchTime').val(time);

	//点击搜索按钮
	$("#searchBtn").click(function() {
		var searchTime = $("#searchTime").val();
		var keyword = $("#keyword").val();
		init(1, 1, keyword, "", searchTime);
	});

	//搜索输入框回车事件
	$("#keyword").keydown(function(e) {
		if(e.keyCode == 13) {
			$('#searchBtn').click();
		}
	});


	//根据状态查看审核列表
	$("#selState").change(function() {
		var stateVal = $("#selState option:selected").val();
		var stateKey = "";
		var searchTime = $("#searchTime").val();
		if(stateVal == 0) {
			stateKey = "未审核";
		}else if(stateVal == 1) {
			stateKey = "已审核";
		}
		init(1, 1, "", stateKey, searchTime);
	});

	// $('#showAmount').change(function() {
	// 	var item = $("#showAmount  option:selected").text();
	// 	init(1, "", "");
	// });
	
	//初始化员工列表，默认显示本月考核列表
	var init = function(cpage,cpage2, keyword,stateKey, searchTime) {
		// var item = $("#showAmount option:selected").text();
		var item = 10;
		var time = year_today + "-" + month_today;
		
		var sendData = {
			stateKey: stateKey,
			time: time,
			item: item,//每页显示10条数据
			index: cpage
		};
		
		// var perfSend = {
		// 	item: item,
		// 	index: cpage,
		// 	keyword: keyword,
		// 	searchTime: searchTime
		// };
		
		$.ajax({
			url: '/api/performance/selMonthPerfsByAss?',
			type: 'get',
			data: {'sendData':JSON.stringify(sendData), '_t':new Date().getTime()},
			success: function(data) {
				// console.info(data);
				// console.info(data.count);
				// $("#showCount").html("共"+ data.count + "条记录");
				var page;
				if (data.count < item) {
					page = 1;
				} else {
					page = Math.ceil(data.count / item);
				}

				var options = {
					currentPage: cpage,
					totalPages: page,
					alignment: 'right',
					count:data.count,
					selectorId: 'pageSeletct',
					onPageChanged: function(e, oldPage, newPage) {
						init(newPage,1, keyword, stateKey, searchTime);
					}
				};

				$('#paginator').bootstrapPaginator(options);
				$("#pageSeletct").change(function() {
					var page = $("#pageSeletct option:selected").val();
					$('#paginator').bootstrapPaginator("show",page);
				});
				showList(data.data);

			},
			error: function(result) {
				alert('error');
			}

		});

		initAss(item, cpage2, keyword, searchTime);
		// $.ajax({
		// 	url: '/api/performance/selPerfsByAss?',
		// 	type: 'get',
		// 	data: {'item':item,'index':cpage2,'keyword':keyword,'searchTime':searchTime, '_t':new Date().getTime()},
		// 	success: function(results) {
		// 		// console.info(results);
		// 		// $("#showCount2").html("共"+ results.count + "条记录");
		// 		var page;
		// 		if (results.count < item) {
		// 			page = 1;
		// 		} else {
		// 			page = Math.ceil(results.count / item);
		// 		}

		// 		var options = {
		// 			currentPage: cpage2,
		// 			totalPages: page,
		// 			alignment: 'right',
		// 			count:results.count,
		// 			selectorId: 'pageSeletct2',
		// 			onPageChanged: function(e, oldPage, newPage) {
		// 				init(1,newPage, keyword, stateKey, searchTime);
		// 			}
		// 		};

		// 		$('#paginator2').bootstrapPaginator(options);
		// 		$("#pageSeletct2").change(function() {
		// 			var page = $("#pageSeletct2 option:selected").val();
		// 			$('#paginator2').bootstrapPaginator("show",page);
		// 		});
		// 		showPerfList(results.data);
		// 	},
		// 	error: function(results) {
		// 		console.info('error---selPerfsByAss');
		// 	}
		// });
		
	};
      
    var initAss = function(item, cpage2, keyword, searchTime) {
    	$.ajax({
    		url: '/api/performance/selPerfsByAss?',
    		type: 'get',
    		data: {'item':item,'index':cpage2,'keyword':keyword,'searchTime':searchTime, '_t':new Date().getTime()},
    		success: function(results) {
    			// console.info(results);
    			// $("#showCount2").html("共"+ results.count + "条记录");
    			var page;
    			if (results.count < item) {
    				page = 1;
    			} else {
    				page = Math.ceil(results.count / item);
    			}

    			var options = {
    				currentPage: cpage2,
    				totalPages: page,
    				alignment: 'right',
    				count:results.count,
    				selectorId: 'pageSeletct2',
    				onPageChanged: function(e, oldPage, newPage) {
    					// init(1,newPage, keyword, stateKey, searchTime);
    					initAss(item, newPage, keyword, searchTime);
    				}
    			};

    			$('#paginator2').bootstrapPaginator(options);
    			$("#pageSeletct2").change(function() {
    				var page = $("#pageSeletct2 option:selected").val();
    				$('#paginator2').bootstrapPaginator("show",page);
    			});
    			showPerfList(results.data);
    		},
    		error: function(results) {
    			console.info('error---selPerfsByAss');
    		}
    	});
    }

	var hisPage = $("#hisPage").val();
	var keyword = "";
	var stateKey = "";
	var searchTime = time;
	var key = $("#getKey").val();
	// console.info("key", $("#getKey"));
	if(key != "" && key != null && key != undefined) {
		key = JSON.parse(decodeURIComponent(key));
	}
	// console.info("key=" + key);
	if(hisPage == "" || hisPage == 0 || hisPage == null || hisPage == undefined) {
		hisPage = 1;
		// stateKey = key.stateKey;
	}else{
		// stateKey = key.stateKey;
		$("#demo2").removeClass("active");
		$("#demo").addClass("active");
		$("#demo3").removeClass("active");
		
	}
	var hisPage2 = $("#hisPage2").val();
	if(hisPage2 == "" || hisPage2 == 0 || hisPage2 == null || hisPage2 == undefined) {
		hisPage2 = 1;
		
	}else{
		keyword = key.keyword;
		searchTime = key.searchTime;
		var type = key.type;
		if(type == 2) {
			$("#demo").removeClass("active");
			$("#demo2").addClass("active");
			$("#demo3").removeClass("active");
		}
		if(type == 3) {
			$("#demo").removeClass("active");
			$("#demo2").removeClass("active");
			$("#demo3").addClass("active");
		}
		$("#searchTime").val(searchTime);
		// $("#demo").removeClass("active");
		// $("#demo2").addClass("active");
		$("#assList").removeClass("active");
		$("#perfList").addClass("active");
	}

	init(hisPage,hisPage2,keyword, stateKey, searchTime);

	var showPerfList = function(data) {
		$("#tbd_perf").html("");
		if(data == null || data.length == 0) {
			$('#tip2').html("<div class='alert alert-warning' style='margin-top:-20px;'>未能搜索到相关信息</div>");
		}else{
			$("#tip2").html("");
		}
		$.each(data, function(i, item) {
			// console.info(i +"---:" + item.state);
			var showState;
			if(item.state == 2) {
				showState = "已确认";
			}else{
				showState = "已审核";
			}
			
			var templateData = {
				perfId : item.id,
				orgName: item.orgName,
				projectDept: item.projectDept,
				perfDate: item.perfDate,
				empName: item.empName,
				showState: showState
			};
			var template = "<tr><td class='center' >{{empName}}</td>"+
			        "<input type='hidden' id='performanceId' name='performanceId' value={{perfId}} />"+
					"<td class='center' >{{orgName}}</td>"+
					"<td class='center' >{{projectDept}}</td>"+
					"<td class='center' >{{perfDate}}</td>"+
					"<td class='center' >{{showState}}</td>"+
					"<td style='cursor:pointer;' class=center id=checkPerf><i class='icon-edit bigger-150'></i>&nbsp;查看详细</td></tr>";

			var $html = $(Mustache.to_html(template, templateData));
			$("#tbd_perf").append($html);

			$html.find('#checkPerf').click({
				html: $html
			}, function(e) {
				var $html = $(e.data.html);
				var performanceId = $html.find("#performanceId").val();
				// alert(performanceId);
				var cpage2 = $("#pageSeletct2 option:selected").val();
				
				var key = {
					searchTime: $("#searchTime").val(),
					keyword: $("#keyword").val()
				};
				// console.info("demo2", $("#demo2").hasClass("active"));
				// console.info("demo3", $("#demo3").hasClass("active"));
				// return;
				if($("#demo2").hasClass("active")) {
					key.type = 2;
				}
				if($("#demo3").hasClass("active")) {
					key.type = 3;
				}
				
                // console.info(encodeURIComponent(JSON.stringify(key)));
                key = encodeURIComponent(JSON.stringify(key));
                
				window.location.href = "/performanceAudit/staffPerformance/" + performanceId + "?cpage=" + cpage2 + "&key=" + key;
			});
		});

	}

	var showList = function(data) {
		// console.info("showList:",data);
		$("#tbd").html("");
		//每月的23-26号才可以审核绩效
		// if(today.getDate() < 23 || today.getDate() > 26) {
		// 	$('#tip').html("<div class='alert alert-warning' style='margin-top:-20px;'>每月23-26为绩效审核时间，其余时间不能审核</div>");
		// 	$('#paginator').css("display", "none");
		// 	return;
		// }
		if(data == null || data.length == 0) {
			$('#tip').html("<div class='alert alert-warning' style='margin-top:-20px;'>未能搜索到相关信息</div>");
		}else{
			$("#tip").html("");
		}
		$.each(data, function(i, item) {
			var showState;
			if(item.termState == 0) {
				showState = "待审核";
			}else if(item.termState == 1) {
				// showState = item.score + "(" + item.level + ")";
				showState = "已审核";
			}
			var templateData = {
				perfId : item.id,
				orgName: item.orgName,
				projectDept: item.projectDept,
				perfDate: item.perfDate,
				empName: item.empName,
				showState: showState
			};
			var template = "<tr><td class='center' >{{empName}}</td>"+
			        "<input type='hidden' id='perfId' name='perfId' value={{perfId}} />"+
					"<td class='center' >{{orgName}}</td>"+
					"<td class='center' >{{projectDept}}</td>"+
					"<td class='center' >{{perfDate}}</td>"+
					"<td class='center' >{{showState}}</td>"+
					"<td class=center id=auditPerf style='cursor:pointer;'><i class='icon-edit bigger-150' ></i>&nbsp;审核</td></tr>";

			var $html = $(Mustache.to_html(template, templateData));
			$("#tbd").append($html);

			$html.find('#auditPerf').click({
				html: $html
			}, function(e) {
				var $html = $(e.data.html);
				var perfId = $html.find("#perfId").val();
				var cpage = $("#pageSeletct option:selected").val();
				window.location.href = "/performanceAudit/staffAudit?perfId=" + perfId + "&termState=" + item.termState + "&cpage=" + cpage;
				// alert(perfId);
			});
		});


	};

	$("#demo2").click(function() {
		$("#searchTime").val(time);
		$("#assList").removeClass("active");
		$("#perfList").addClass("active");
		initAss(10, 1, "", time);
	});
	$("#demo3").click(function() {
		var nextMonth = "";
		var year_search = year_today;
		if(month_today == 12) {
			nextMonth = "01";
			year_search += 1;
		}else{
			nextMonth = (today.getMonth() + 2) < 10 ? "0" + (today.getMonth() + 2) : (today.getMonth() + 2);
		}
		var searchTime = year_search + "-" + nextMonth;
		$("#searchTime").val(searchTime);
		$("#assList").removeClass("active");
		$("#perfList").addClass("active");
		initAss(10, 1, "", searchTime);
	});
	
});